﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using ProductIntroduction.Models;

namespace ProductIntroduction.Areas.Admin.Controllers
{ 
    public class ProductController : Controller
    {
        private DBEntities db = new DBEntities();

        //
        // GET: /Admin/Product/

        public ViewResult Index()
        {
            var products = db.Products.Include("Category").Include("Manufacturer");
            return View(products.ToList());
        }

        //
        // GET: /Admin/Product/Details/5

        public ViewResult Details(int id)
        {
            Product product = db.Products.Single(p => p.ProductId == id);
            return View(product);
        }

        //
        // GET: /Admin/Product/Create

        public ActionResult Create()
        {
            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "CategoryName");
            ViewBag.ManufacturerId = new SelectList(db.Manufacturers, "ManufacturerId", "ManufacturerName");
            return View();
        } 

        //
        // POST: /Admin/Product/Create

        [HttpPost]
        public ActionResult Create(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.AddObject(product);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "CategoryName", product.CategoryId);
            ViewBag.ManufacturerId = new SelectList(db.Manufacturers, "ManufacturerId", "ManufacturerName", product.ManufacturerId);
            return View(product);
        }
        
        //
        // GET: /Admin/Product/Edit/5
 
        public ActionResult Edit(int id)
        {
            Product product = db.Products.Single(p => p.ProductId == id);
            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "CategoryName", product.CategoryId);
            ViewBag.ManufacturerId = new SelectList(db.Manufacturers, "ManufacturerId", "ManufacturerName", product.ManufacturerId);
            return View(product);
        }

        //
        // POST: /Admin/Product/Edit/5

        [HttpPost]
        public ActionResult Edit(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.Attach(product);
                db.ObjectStateManager.ChangeObjectState(product, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "CategoryName", product.CategoryId);
            ViewBag.ManufacturerId = new SelectList(db.Manufacturers, "ManufacturerId", "ManufacturerName", product.ManufacturerId);
            return View(product);
        }

        //
        // GET: /Admin/Product/Delete/5
 
        public ActionResult Delete(int id)
        {
            Product product = db.Products.Single(p => p.ProductId == id);
            return View(product);
        }

        //
        // POST: /Admin/Product/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Product product = db.Products.Single(p => p.ProductId == id);
            db.Products.DeleteObject(product);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Test()
        {
            return View();
        }

        public ContentResult Upload()
        {
            Thread.Sleep(5000);
            for (int i = 0; i < Request.Files.Count; i++ )
            {
                HttpPostedFileBase file = Request.Files[i];
                // Verify that the user selected a file
                if (file != null && file.ContentLength > 0)
                {
                    // extract only the fielname
                    var fileName = DateTime.Now.ToString("yyyyMMdd_HHmmss_") + Path.GetFileName(file.FileName);
                    // store the file inside ~/Upload/Banner folder
                    var path = Path.Combine(Server.MapPath("~/Upload/Banner/"), fileName);
                    file.SaveAs(path);

                    var fileUrl = string.Empty;
                    if (Request.Url.Port == 0)
                    {
                        fileUrl = "http://" + Request.Url.Host + "/Upload/Banner/" + fileName;
                    }
                    else
                    {
                        fileUrl = "http://" + Request.Url.Host + ":" + Request.Url.Port + "/Upload/Banner/" + fileName;
                    }
                }
            }


            //return Content("{ FilePath = '" + fileUrl + "', FileName = '" + fileName + "' }");

            return Content("{ Success = true }");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}